package com.mav.service.iml;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mav.mapper.EmpExprMapper;
import com.mav.mapper.EmpMapper;
import com.mav.pojo.*;
import com.mav.service.EmpLogService;
import com.mav.service.EmpService;
import com.mav.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;

@Slf4j
@Service
public class EmpServiceIml implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private EmpLogService empLogService;
    @Override
    public PageResult<Emp> page(EmpQueryParam empQueryParam) {
        PageHelper.startPage(empQueryParam.getPage(),empQueryParam.getPageSize());
        List<Emp> empList = empMapper.list(empQueryParam.getName(),empQueryParam.getGender(),empQueryParam.getBegin(),empQueryParam.getEnd());
        Page<Emp> page1=(Page<Emp>) empList;
        return new PageResult<Emp>(page1.getTotal(),page1.getResult());
    }
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void save(Emp emp) {
        try {
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.insert(emp);
            List<EmpExpr> empExprList = emp.getExprList();
               log.info("获得的员工id:{}",emp.getId());
            if(!empExprList.isEmpty()){
                empExprList.forEach(empExpr->{

                    empExpr.setEmpId(emp.getId());
                });
                empExprMapper.insertBatch(empExprList);
            }
        } finally {
            EmpLog empLog=new EmpLog(null,LocalDateTime.now(),"新增员工:"+emp);
            empLogService.insertLog(empLog);
        }
    }
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void delete(List<Integer> ids) {
        empMapper.deleteById(ids);
        empExprMapper.deleteByEmpId(ids);
    }

    @Override
    public Emp getInfo(Integer id) {
        return empMapper.getById(id);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
        empExprMapper.deleteByEmpId(Collections.singletonList(emp.getId()));
        List<EmpExpr> exprList = emp.getExprList();
        if (!exprList.isEmpty()){
            exprList.forEach(empExpr -> {
                empExpr.setEmpId(emp.getId());
            });
            empExprMapper.insertBatch(exprList);
        }
    }

    @Override
    public List<Emp> findAll() {
        return empMapper.findAll();
    }

    @Override
    public LoginInfo getLoginInfo(Emp emp) {
        Emp e=empMapper.getByUsernameAndPassword(emp);

        if(e!=null){
            Map<String,Object> claims=new HashMap<>();
            claims.put("id",e.getId());
            claims.put("username",e.getUsername());
            String token = JwtUtils.generateToken(claims);
            return new LoginInfo(e.getId(),e.getUsername(),e.getPassword(),e.getName(),token);
        }
        return null;
    }


    /*
    原始
    @Override
    public PageResult<Emp> page(Integer page, Integer pageSize) {
        Integer start=(page-1)*pageSize;
        List<Emp> list = empMapper.list(start, pageSize);
        Long total= empMapper.count();
        return new PageResult<Emp>(total, list);
    }*/
}
